<template>
    <!--<div class="app-container calendar-list-container">-->
        <!--<div class="filter-container">-->
            <!--<el-button v-if="cm_column_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加-->
            <!--</el-button>-->
        <!--</div>-->
      <div class="app-container pull-auto">
        <!--<el-button type="primary" @click="handleAdd" size="small" v-if="permissions.cm_column_add">新 增</el-button>-->
        <!--<br /><br />-->
        <avue-crud ref="crud" :page="page" :data="tableData" :table-loading="tableLoading" :option="tableOption" @current-change="currentChange" @size-change="sizeChange" @row-update="handleUpdate" @row-save="handleSave" @row-del="rowDel">
          <template slot-scope="scope" slot="menuLeft">
            <el-button type="primary"
                       icon="el-icon-plus"
                       size="small"
                       v-if="permissions.cm_column_add"
                       plain
                       @click.stop="handleAdd()">新增</el-button>
          </template>
          <template slot-scope="scope" slot="menu">
            <el-button type="primary" v-if="permissions.cm_column_upd" icon="el-icon-check" size="small" plain @click="handleEdit(scope.row,scope.index)">编辑</el-button>
            <el-button type="danger" v-if="permissions.cm_column_del" icon="el-icon-delete" size="small" plain @click="handleDel(scope.row,scope.index)">删除</el-button>
          </template>
        </avue-crud>
      </div>
        <!--<el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit-->
                  <!--highlight-current-row style="width: 100%">-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.id }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.name }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.parentId }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.code }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.homeUrl }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.type }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.template }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.isDelete }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.makeDate }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column align="center" label="">-->
                <!--<template scope="scope">-->
                    <!--<span>{{ scope.row.remark }}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->

            <!--<el-table-column label="操作">-->
                <!--<template scope="scope">-->
                    <!--<el-button v-if="cm_column_upd" size="small" type="success"-->
                               <!--@click="handleUpdate(scope.row)">编辑-->
                    <!--</el-button>-->
                    <!--<el-button v-if="cm_column_del" size="mini" type="danger"-->
                               <!--@click="handleDelete(scope.row)">删除-->
                    <!--</el-button>-->
                <!--</template>-->
            <!--</el-table-column>-->
        <!--</el-table>-->
        <!--<div v-show="!listLoading" class="pagination-container">-->
            <!--<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"-->
                           <!--:current-page.sync="listQuery.page"-->
                           <!--:page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"-->
                           <!--layout="total, sizes, prev, pager, next, jumper" :total="total">-->
            <!--</el-pagination>-->
        <!--</div>-->
        <!--<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">-->
            <!--<el-form :model="form" :rules="rules" ref="form" label-width="100px">-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.id" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.name" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.parentId" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.code" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.homeUrl" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.type" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.template" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.isDelete" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.makeDate" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
                    <!--<el-form-item label="" prop="username">-->
                        <!--<el-input v-model="form.remark" placeholder=""></el-input>-->
                    <!--</el-form-item>-->
            <!--</el-form>-->
            <!--<div slot="footer" class="dialog-footer">-->
                <!--<el-button @click="cancel('form')">取 消</el-button>-->
                <!--<el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>-->
                <!--<el-button v-else type="primary" @click="update('form')">修 改</el-button>-->
            <!--</div>-->
        <!--</el-dialog>-->
    <!--</div>-->
</template>

<script>
    import { fetchList, addObj, putObj, delObj } from '@/api/cmColumn.js'
    import waves from '@/directive/waves/index.js' // 水波纹指令
    import { tableOption } from '@/const/crud/cmColumn.js'
    import { mapGetters } from 'vuex'

    export default {
      name: 'table_cm_column',
      directives: {
        waves
      },
      data() {
        return {
          list: null,
          total: null,
          listLoading: true,
          listQuery: {
            page: 1,
            limit: 20
          },
          page: {
            total: 0, // 总页数
            currentPage: 1, // 当前页数
            pageSize: 20 // 每页显示多少条
          },
          rules: {
          },
          form: {
          },
          tableData: [],
          tableOption: tableOption,
          dialogFormVisible: false,
          dialogStatus: '',
          tableLoading: false,
          cm_column_add: false,
          cm_column_upd: false,
          cm_column_del: false,
          textMap: {
            update: '编辑',
            create: '创建'
          },
          tableKey: 0
        }
      },
      computed: {
        ...mapGetters([
          'permissions'
        ])
      },
      filters: {
        statusFilter(status) {
          const statusMap = {
            0: '有效',
            1: '无效'
          }
          return statusMap[status]
        }
      },
      created() {
        this.getList()
        this.cm_column_add = this.permissions['cm_column_add']
        this.cm_column_upd = this.permissions['cm_column_upd']
        this.cm_column_del = this.permissions['cm_column_del']
      },
      methods: {
        getList() {
          this.tableLoading = true
          fetchList({
            page: this.page.currentPage,
            limit: this.page.pageSize
          }).then(response => {
            this.tableData = response.data.records
            this.page.total = response.data.total
            this.tableLoading = false
          })
        },
        currentChange(val) {
          this.page.currentPage = val
          this.getList()
        },
        sizeChange(val) {
          this.page.pageSize = val
          this.getList()
        },
        /**
         * @title 打开新增窗口
         * @detail 调用crud的handleadd方法即可
         *
         **/
        handleAdd: function() {
          this.$refs.crud.rowAdd()
        },
        handleEdit(row, index) {
          this.$refs.crud.rowEdit(row, index)
        },
        handleDel(row, index) {
          this.$refs.crud.rowDel(row, index)
        },
        rowDel: function(row, index) {
          var _this = this
          this.$confirm('是否确认删除ID为' + row.id, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(function() {
              return delObj(row.id)
            })
            .then(data => {
              _this.tableData.splice(index, 1)
              this.getList()
              _this.$message({
                showClose: true,
                message: '删除成功',
                type: 'success'
              })
            })
            .catch(function(err) {})
        },
        /**
         * @title 数据更新
         * @param row 为当前的数据
         * @param index 为当前更新数据的行数
         * @param done 为表单关闭函数
         *
         **/
        handleUpdate: function(row, index, done) {
          putObj(row).then(data => {
            this.tableData.splice(index, 1, Object.assign({}, row))
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            })
            done()
            this.getList()
          })
        },
        /**
         * @title 数据添加
         * @param row 为当前的数据
         * @param done 为表单关闭函数
         *
         **/
        handleSave: function(row, done) {
          addObj(row).then(data => {
            this.tableData.push(Object.assign({}, row))
            this.$message({
              showClose: true,
              message: '添加成功',
              type: 'success'
            })
            done()
            this.getList()
          })
        }
      }
    }

    //     getList() {
    //       this.listLoading = true
    //       fetchList(this.listQuery).then(response => {
    //         this.list = response.data.records
    //         this.total = response.data.total
    //         this.listLoading = false
    //       })
    //     },
    //     handleSizeChange(val) {
    //       this.listQuery.limit = val
    //       this.getList()
    //     },
    //     handleCurrentChange(val) {
    //       this.listQuery.page = val
    //       this.getList()
    //     },
    //     handleDelete(row) {
    //       delObj(row.id)
    //                     .then(response => {
    //                       this.dialogFormVisible = false
    //                       this.getList()
    //                       this.$notify({
    //                         title: '成功',
    //                         message: '删除成功',
    //                         type: 'success',
    //                         duration: 2000
    //                       })
    //                     })
    //     },
    //     handleCreate() {
    //       this.dialogStatus = 'create'
    //       this.dialogFormVisible = true
    //     },
    //     create(formName) {
    //       const set = this.$refs
    //       set[formName].validate(valid => {
    //         if (valid) {
    //           addObj(this.form)
    //                             .then(() => {
    //                               this.dialogFormVisible = false
    //                               this.getList()
    //                               this.$notify({
    //                                 title: '成功',
    //                                 message: '创建成功',
    //                                 type: 'success',
    //                                 duration: 2000
    //                               })
    //                             })
    //         } else {
    //           return false
    //         }
    //       })
    //     },
    //     cancel(formName) {
    //       this.dialogFormVisible = false
    //       const set = this.$refs
    //       set[formName].resetFields()
    //     },
    //     update(formName) {
    //       const set = this.$refs
    //       set[formName].validate(valid => {
    //         if (valid) {
    //           this.dialogFormVisible = false
    //           this.form.password = undefined
    //           putObj(this.form).then(() => {
    //             this.dialogFormVisible = false
    //             this.getList()
    //             this.$notify({
    //               title: '成功',
    //               message: '修改成功',
    //               type: 'success',
    //               duration: 2000
    //             })
    //           })
    //         } else {
    //           return false
    //         }
    //       })
    //     }
    //   }
    // }
</script>
